package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.RecommendSocietyActivity;
import reduce.fz.com.reduce.activity.SocietyCreateActivity;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.event.SocietyEvent;
import reduce.fz.com.reduce.fragment.base.BaseFragment;
import reduce.fz.com.reduce.service.SocietyService;
import reduce.fz.com.reduce.util.ServiceBuilder;

/**
 * 我的社团(申请创建，搜索，以及社团列表)
 */
public class MySocietyFragment extends BaseFragment implements View.OnClickListener,FragmentCacheSupport {
    private TextView mCreate;
    private ImageView mFind;
    private TextView mBCreate;
    private TextView mBFind;
    private SocietyListFragment fragment;
    private RelativeLayout mRlJoined;
    private RelativeLayout mRlNoJoined;
    private SocietyService societyService = ServiceBuilder.builderSocietyService(null);

    public static MySocietyFragment create() {
        MySocietyFragment fragment = new MySocietyFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    public MySocietyFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }
    /**
     * 时间监听
     *
     * @param event
     */
    public void onEvent(SocietyEvent event) {
        if (event.code.equals(event.CODE_ADD)) {
            fragment.addFrist(event.society);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void isJoined() {
        societyService.getJoinedSocietyNum(new AbResponseListener<Long>(new TypeReference<Result<Long>>() {
        }) {
            @Override
            public void error(ServiceException e) {

            }

            @Override
            public void responseInfo(Result<Long> data) {
                Long num = data.getData();
                if (num == 0) {
                    mRlJoined.setVisibility(View.GONE);
                    mRlNoJoined.setVisibility(View.VISIBLE);
                } else {
                    mRlJoined.setVisibility(View.VISIBLE);
                    mRlNoJoined.setVisibility(View.GONE);
                    attachDynamicListFragment();
                }

            }
        });

    }


    public void initWidget(View itemView) {
        mRlJoined = (RelativeLayout) itemView.findViewById(R.id.rl_joined);
        mRlNoJoined = (RelativeLayout) itemView.findViewById(R.id.rl_nojoined);
        mCreate = (TextView) itemView.findViewById(R.id.tv_create);
        mFind = (ImageView) itemView.findViewById(R.id.iv_find);
        mBCreate = (TextView) itemView.findViewById(R.id.tv_bcreate);
        mBFind = (TextView) itemView.findViewById(R.id.tv_bfind);
        mCreate.setOnClickListener(this);
        mFind.setOnClickListener(this);
        mBCreate.setOnClickListener(this);
        mBFind.setOnClickListener(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_society, container, false);
        initWidget(view);
        isJoined();
        return view;
    }

    public void attachDynamicListFragment() {
        fragment = SocietyListFragment.createMySocietyListFragment(null);
        getChildFragmentManager().beginTransaction().replace(R.id.socity_container, fragment).commit();
    }

    /**
     * 跳转到创建页面
     */
    public void forwardCreate() {
        Intent intent = new Intent(getActivity(), SocietyCreateActivity.class);
        startActivity(intent);
    }

    /*跳转到
     */
    public void forwardFind() {
        Intent intent = new Intent(getActivity(), RecommendSocietyActivity.class);
        startActivity(intent);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_create:
            case R.id.tv_bcreate:
                forwardCreate();
                break;
            case R.id.iv_find:
            case R.id.tv_bfind:
                forwardFind();
                break;
            default:
                break;
        }
    }

    @Override
    public String getCacheName() {
        return "mysociety";
    }

    @Override
    public String getFragmentTitle() {
        return "我的社团";
    }
}
